package com.example.androidcomplete0102.ch16;



import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.androidcomplete0102.R;

public class OrderDialogFrg extends Activity implements CustomDialogFragment.NoticeDialogListener{
	TextView tv;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ch16_dailogtest);
		tv = (TextView)findViewById(R.id.text);
	}
	
	public void mOnClick(View v){
		CustomDialogFragment Frg = new CustomDialogFragment();
		Bundle bundle = new Bundle();
		bundle.putString("title", "주문정보를 입력하세요");
		Frg.setArguments(bundle);
		Frg.show(getFragmentManager(), "dialog");
	}

	@Override
	public void onDialogClick(DialogFragment dialog, String res) {
		tv.setText(res);
	}
	
}

class CustomDialogFragment extends DialogFragment{
	NoticeDialogListener mListener;
	Bundle bundle;
	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		try{
			mListener = (NoticeDialogListener)activity;
		}catch(ClassCastException e){
			throw new ClassCastException(activity.toString());
		}
	}
	
	public interface NoticeDialogListener{
		public void onDialogClick(DialogFragment dialog, String res);
	}
	
	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {
		bundle = getArguments();
		LayoutInflater inflater = getActivity().getLayoutInflater();
		final LinearLayout linear = (LinearLayout)inflater.inflate(R.layout.ch16_order, null);
		
		return new AlertDialog.Builder(getActivity())
			.setIcon(R.drawable.ic_launcher)
			.setTitle(bundle.getString("title"))
			.setView(linear)
			.setPositiveButton("확인", new OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					EditText product = (EditText)linear.findViewById(R.id.product);
					EditText number = (EditText)linear.findViewById(R.id.number);
					CheckBox paymethod = (CheckBox)linear.findViewById(R.id.paymethod);
					StringBuilder sb = new StringBuilder("주문정보\n");
					sb.append(product.getText()+" ");
					sb.append(number.getText()+"개");
					sb.append(paymethod.isChecked()?"착불결제":"");
					mListener.onDialogClick(CustomDialogFragment.this, sb.toString());
							
				}
			})
			.setNegativeButton("취소", new OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					mListener.onDialogClick(CustomDialogFragment.this, "주문을 취소했습니다.");
				}
			})
			.create();
	}
}
